<?php

namespace App\Models;

class CashOuts extends Base
{
    protected $table = 'cash_outs';

    const STATUS_1 = 1;
    const STATUS_2 = 2;
    const STATUS_3 = 3;
    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag el-tag--danger">未处理</span>',
        self::STATUS_2 => '<span class="el-tag">已支付</span>',
    	self::STATUS_3 => '<span class="el-tag">已驳回</span>',
    ];

    const PAYTYPE_1 = 1;
    const PAYTYPE_2 = 2;
    const PAYTYPE_3 = 3;
    
    const PAY_TYPE_TEXT = [
    		self::PAYTYPE_1 => '银行卡',
    		self::PAYTYPE_2 => '支付宝',
    		self::PAYTYPE_3 => '微信',
    ];
    
    protected $fillable = [
        'id','pay_sn','user_id','user_name','phone','amount','bank_name','bank_no','bank_user','status',
        'payment_admin','payment_time','created_at','updated_at','pay_type','qrcode','paycount','useramount'
    ];

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('user_name','like','%' . $params['kwd'] . '%')
                           ->orwhere('payment_admin','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
                if(isset($params['start']) && $params['start']) {
                    $query ->whereDate('created_at','>=',$params['start']);
                }
                if(isset($params['end']) && $params['end']) {
                    $query ->whereDate('created_at','<=',$params['end']);
                }
            })->where('status','<>',-1)
            ->when($params, function ($query) use($params){
                if(isset($params['sort']) && $params['sort']) {
                    return $query->orderBy('created_at',$params['sort']);
                }
            })->orderBy('created_at', 'desc')
            ->paginate($limit);

        $this->stateToText($lists,[
            'status'=> self::STATUS_TEXT,
        ]);
        
        $this->stateToText($lists,[
        	'pay_type'=> self::PAY_TYPE_TEXT,
        ]);
        return $lists;
    }

    /**
     * 获取导出数据
     * @param array $params 条件参数
     * @return mixed
     */
    protected function exports($params){
        $fields = [
            'id','pay_sn','user_name','amount','bank_name','bank_no','bank_user',
            'status','payment_admin','payment_time','created_at'
        ];
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params) && $params) {
                    $ids = explode('-',$params);
                    $query ->whereIn('id',$ids);
                }
            })->where('status','<>',-1)->get();

        foreach ($lists as $k=>$v){
            if($v->attributes['status'] == 1){
                $v->attributes['status'] = '未支付';
            }else{
                $v->attributes['status'] = '已支付';
            }
        }
        return $lists;
    }

}
